//
//  ZYVSAutoModeSettingView.m
//  YiLin
//
//  Created by Zyark on 2018/6/6.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYVSAutoModeSettingView.h"
#import "ZYImageUtils.h"
#import "ZYModeTypeModel.h"

@interface ZYVSAutoModeSettingView()
/**
 模式图片
 */
@property(nonatomic,strong) UIImageView *modeTypeImageView;

/**
 模式名称
 */
@property(nonatomic,strong) UILabel *modeNameLabel;

/**
 新风状态
 */
@property(nonatomic,strong) UILabel *commonStatusLabel;

/**
 净化状态
 */
@property(nonatomic,strong) UILabel *purificationStatusLabel;

/**
 睡眠状态按钮
 */
@property(nonatomic,strong) UILabel *sleepingStatusLabel;


/**
 新风状态按钮
 */
@property(nonatomic,strong) UIButton *commonStatusButton;

/**
 净化状态按钮
 */
@property(nonatomic,strong) UIButton *purificationStatusButton;

/**
 睡眠状态按钮
 */
@property(nonatomic,strong) UIButton *sleepingStatusButton;


/**
 状态背景
 */
@property(nonatomic,strong) UIView *bgView;


/**
 底部图片
 */
@property(nonatomic,strong) UIImageView *bottomImageView;


/**
 顶部图片
 */
@property(nonatomic,strong) UIImageView *topImageView;

/**
 中间图片
 */
@property(nonatomic,strong) UIImageView *middleImageView;
@end

@implementation ZYVSAutoModeSettingView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    [self setupView];
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _modeNameLabel.text = [language getStringByKey:LC_VS_MODE_TYPE_AUTO];
    _modeTypeImageView.image = [UIImage imageNamed:@"ic_vs_mode_type_auto_unselected"];
    self.startTime = @"23:00";
    self.endTime = @"7:00";
    self.purificationTime = 30;
    self.speedType = ZYWindSpeedTypeLow;
}

-(void)setupView{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    CGFloat imageWidth = self.width * 0.13;
    //模式图片
    _modeTypeImageView = [[UIImageView alloc] init];
    _modeTypeImageView.size = CGSizeMake(imageWidth, imageWidth);
    [self addSubview:_modeTypeImageView];
    _modeTypeImageView.left = 17;
    _modeTypeImageView.top = 15;
    
    //模式名称
    _modeNameLabel = [[UILabel alloc] init];
    _modeNameLabel.textColor = kMainColor;
    _modeNameLabel.font = [UIFont systemFontOfSize:14];
    _modeNameLabel.size = CGSizeMake(imageWidth * 3, imageWidth  * 0.4);
    [self addSubview:_modeNameLabel];
    _modeNameLabel.top = _modeTypeImageView.top;
    _modeNameLabel.left = _modeTypeImageView.right + 20;
    
    _bgView = [[UIView alloc] init];
    _bgView.size = CGSizeMake(self.width - _modeTypeImageView.right - 10,self.height * 4 / 5);
    [self addSubview:_bgView];
    _bgView.top = _modeNameLabel.bottom + 15;
    _bgView.left = _modeTypeImageView.right;
    
    CGFloat margin = 2;
    // 净化状态
    _purificationStatusLabel = [self setupStatusLabel:[language getStringByKey:LC_VS_STATUS_PURIFICATION]];
    _purificationStatusLabel.centerX = self.centerX - _bgView.left;
    _purificationStatusLabel.top = 0;
    
     // 净化状态按钮
    _purificationStatusButton = [self setupButton];
    _purificationStatusButton.centerY = _purificationStatusLabel.centerY;
    _purificationStatusButton.left = _purificationStatusLabel.right + 3;
    _purificationStatusButton.width = 52;
    
    //顶部图片
    _topImageView = [self setupImageView:@"ic_vs_status_edit_top"];
    _topImageView.right = _purificationStatusLabel.right;
    _topImageView.top = _purificationStatusLabel.bottom + margin;
    
    //中间图片
    _middleImageView = [self setupImageView:@"ic_vs_status_edit_middle"];
    _middleImageView.centerX = _purificationStatusLabel.centerX;
    _middleImageView.top = _topImageView.bottom + 5;
    
    //新风状态按钮 风速
    _commonStatusButton = [self setupButton];
    _commonStatusButton.centerX = _middleImageView.left;
    _commonStatusButton.top = _middleImageView.bottom + margin;
    _commonStatusButton.width = 52;
    
    //新风状态
    _commonStatusLabel = [self setupStatusLabel:[language getStringByKey:LC_VS_STATUS_COMMON]];
    _commonStatusLabel.centerY = _commonStatusButton.centerY;
    _commonStatusLabel.right = _commonStatusButton.left - margin;
    
    //睡眠状态
    _sleepingStatusLabel = [self setupStatusLabel:[language getStringByKey:LC_VS_STATUS_SLEEPING]];
    if(ZYiPhone5_OR_5c_OR_5s || ZYiPhone4_OR_4s){
        _sleepingStatusLabel.left = _middleImageView.right - 15;
    }else{
        _sleepingStatusLabel.left = _middleImageView.right - 5;
    }
    _sleepingStatusLabel.top = _middleImageView.bottom + margin;
    
    //睡眠状态按钮
    _sleepingStatusButton = [self setupButton];
    _sleepingStatusButton.width = 80;
    _sleepingStatusButton.centerY = _sleepingStatusLabel.centerY;
    _sleepingStatusButton.left = _sleepingStatusLabel.right + margin;
    
    //底部
    _bottomImageView = [self setupImageView:@"ic_vs_status_edit_bottom"];
    _bottomImageView.centerX = _middleImageView.centerX;
    _bottomImageView.top = _commonStatusButton.bottom + margin;
    
    [_purificationStatusButton addTarget:self action:@selector(doPurificationClick:) forControlEvents:UIControlEventTouchUpInside];
    [_sleepingStatusButton addTarget:self action:@selector(doSleepingClick:) forControlEvents:UIControlEventTouchUpInside];
    [_commonStatusButton addTarget:self action:@selector(doCommonClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(UILabel *)setupStatusLabel:(NSString *)title{
    UILabel *label = [[UILabel alloc] init];
    UIFont *font = [UIFont systemFontOfSize:12];
    label.font = font;
    label.textColor = kLabelColor;
    label.text = title;
    label.textAlignment = NSTextAlignmentRight;
    label.height = 13;
    NSDictionary *attributes = @{NSFontAttributeName:font,};
    CGSize size = [title boundingRectWithSize:CGSizeMake(120,13)
                                      options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    label.size = size;
    [_bgView addSubview:label];
    return label;
}

-(UIButton *)setupButton{
    UIButton *button = [[UIButton alloc] init];
    button.size = CGSizeMake(75, 20);
    [_bgView addSubview:button];
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.layer.borderColor = kMainColor.CGColor;
    button.layer.borderWidth = 1;
    [button setTitleColor:kMainColor forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    UIFont *buttonFont = [UIFont systemFontOfSize:12];
    [button.titleLabel setFont:buttonFont];
//    [button setTitle:title forState:UIControlStateNormal];
//    NSDictionary *attributes = @{NSFontAttributeName:buttonFont,};
//    CGSize size = [title boundingRectWithSize:CGSizeMake(100, button.titleLabel.height)
//                                      options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
//    button.width = size.width + 20;
    [button setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:button.bounds] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:button.bounds] forState:UIControlStateNormal];
    return button;
}

-(UIImageView *)setupImageView:(NSString *)imageName{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [_bgView addSubview:imageView];
    return imageView;
}

#pragma mark - Event
/**
 净化状态按钮点击
 @param button 当前按钮
 */
-(void)doPurificationClick:(UIButton *)button{
    if([_delegate respondsToSelector:@selector(vsAutoModeSettingView:doPurificationClick:)]){
        [_delegate vsAutoModeSettingView:self doPurificationClick:button];
    }
}

/**
 睡眠状态按钮点击
 @param button 当前按钮
 */
-(void)doSleepingClick:(UIButton *)button{
    if([_delegate respondsToSelector:@selector(vsAutoModeSettingView:doSleepingClick:)]){
        [_delegate vsAutoModeSettingView:self doSleepingClick:button];
    }
}


/**
 新风状态按钮点击
 @param button 当前按钮
 */
-(void)doCommonClick:(UIButton *)button{
    if([_delegate respondsToSelector:@selector(vsAutoModeSettingView:doCommonClick:)]){
        [_delegate vsAutoModeSettingView:self doCommonClick:button];
    }
}

#pragma mark - public

-(void)setSpeedType:(ZYWindSpeedType)speedType{
    _speedType = speedType;
    NSString *title = @"";
    switch (_speedType) {
        case ZYWindSpeedTypeHeight:
            title = LC_QUICK_SETUP_WIND_SPEED_HEIGHT_LABEL;
            break;
        case ZYWindSpeedTypeMiddle:
            title = LC_QUICK_SETUP_WIND_SPEED_MIDDLE_LABEL;
            break;
        case ZYWindSpeedTypeLow:
            title = LC_QUICK_SETUP_WIND_SPEED_LOW_LABEL;
            break;
        default:
            break;
    }
    [_commonStatusButton setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:title] forState:UIControlStateNormal];
}

-(void)setPurificationTime:(CGFloat)purificationTime{
    _purificationTime = purificationTime;
    NSString *minute = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_QUICK_SETUP_MINUTE_LABEL];
    [_purificationStatusButton setTitle:[NSString stringWithFormat:@"%.0f%@",purificationTime,minute] forState:UIControlStateNormal];
}

-(void)setStartTime:(NSString *)startTime{
    _startTime = startTime;
    [_sleepingStatusButton setTitle:[NSString stringWithFormat:@"%@-%@",_startTime,_endTime] forState:UIControlStateNormal];
}

-(void)setEndTime:(NSString *)endTime{
    _endTime = endTime;
    [_sleepingStatusButton setTitle:[NSString stringWithFormat:@"%@-%@",_startTime,endTime] forState:UIControlStateNormal];
}

- (void)setModel:(ZYModeTypeModel *)model{
    _model = model;
    self.startTime = [NSString stringWithFormat:@"%@:%@",model.startHour,model.startMinute];
    self.endTime = [NSString stringWithFormat:@"%@:%@",model.endHour,model.endMinute];
    self.purificationTime = model.autoMinute;
    self.speedType = model.autoWindSpeedType;
}

@end
